import java.sql.*;
public class Main {
    public static void main(String[] args) {
        showdatabase();
    }
        public static void showdatabase( ){
           Connection conn = null;
           PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                String username = "root";
                String password = "989898";
                String url = "jdbc:mysql://localhost:3306/jdbc";
                // 建立连接
                conn = DriverManager.getConnection(url, username, password);
                // 创建SQL语句执行对象
                String sql="SELECT * FROM sys_user";
                preparedStatement = conn.prepareStatement(sql);
                // 执行查询语句
                resultSet = preparedStatement.executeQuery(sql);
                while (resultSet.next()) {
                    // 根据列名获取数据，这里假设表中有id和name两列
                    int id = resultSet.getInt("id");
                    String name = resultSet.getString("name");
                    String pass = resultSet.getString("pass");
                    System.out.println("id: " + id + ", name: " + name + ", pass: " + pass);
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }finally{
                //关闭资源
                try{
                    if(resultSet != null){
                        resultSet.close();
                    }
                    if(preparedStatement!=null){
                        preparedStatement.close();
                    }
                    if(conn != null){
                        conn.close();
                    }
                }catch(SQLException e){
                    throw new RuntimeException(e);
                }
            }
        }
    }
